local skel = fk.CreateSkill {
  name = "lb__qinwei",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["lb__qinwei"] = "侵闱",
  [":lb__qinwei"] = "<b>锁定技</b>，你与上一张指定目标的牌的首个目标角色使用牌对对方生效时，作废之，" ..
      "观看其手牌并声明其中一个牌名，然后将其手牌中缺少的花色填入<a href=':lb__yunhui'><font color='red'>〖蕴慧〗</font></a>中；" ..
      "若覆盖了现有花色或本回合已被填入的花色，则令此牌使用者本回合使用的下一张基本牌或普通锦囊牌无距离限制且伤害+1，然后清除这些花色并摸等量张牌。",

  ["#lb__qinwei_1"] = "侵闱：声明%s一张手牌的牌名",
  ["#lb__qinwei_2"] = "，将%s填入〖蕴慧〗中",
  ["#lb__qinwei_3"] = "，清除〖蕴慧〗中的%s并摸%d张牌，强化%s使用的下一张牌",

  ["@lb__qinwei"] = "侵闱",
  ["@[player]lb__qinwei-turn"] = "<font color='red'>侵闱</font>",
  ["@[suits]lb__qinwei-turn"] = "<font color='#0598BC'>侵闱</font>",

  ["#lb__qinwei_log"] = "<font color='#0598BC'>〖侵闱〗</font>：%from 观看了 %to 的%arg张手牌，声明了 %arg2",

  ["$lb__qinwei1"] = "这么快就上钩了？",
  ["$lb__qinwei2"] = "百分百弱点击破。",
}

skel:addEffect(fk.CardEffecting, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
      return e.id ~= (player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) or {}).id
      and #e.data.tos > 0
    end, 0)[1]
    if lastUseEvent == nil then return false end
    local last_use = lastUseEvent.data
    if #last_use.tos == 0 then return false end
    local to = last_use.tos[1]
    if to == player then return false end
    if (data.from == player and target == to) or (data.from == to and target == player) then
      event:setCostData(self, { tos = { to } })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data:setNullified(room.players) --先这样吧
    if not data.use.extraUse then
      player:addCardUseHistory(data.card.trueName, -1) -- 作废的牌不计入次数
      data.use.extraUse = true
    end
    local to = event:getCostData(self).tos[1]
    local cards = to:getCardIds("h")
    local suits = player:getTableMark("@[suits]lb__yunhui")
    local insert_suits = { 1, 2, 3, 4 }
    local remove_suits = {}
    local prompt = string.format(Fk:translate("#lb__qinwei_1"), to:__touistring())
    if player:hasSkill("lb__yunhui", true) then
      for _, id in ipairs(cards) do -- 没有的花色
        table.removeOne(insert_suits, Fk:getCardById(id).suit)
      end
      for _, suit in ipairs(table.connect(suits, player:getTableMark("@[suits]lb__qinwei-turn"))) do
        if table.removeOne(insert_suits, suit) and table.removeOne(suits, suit) then
          table.insert(remove_suits, suit)
        end
      end
      if #insert_suits > 0 then
        prompt = prompt ..
            string.format(Fk:translate("#lb__qinwei_2"), table.concat(table.map(insert_suits, function(suit)
              return Fk:translate(Card.getSuitString({ suit = suit }, true))
            end), "/"))
      end
      if #remove_suits > 0 then
        prompt = prompt ..
            string.format(Fk:translate("#lb__qinwei_3"), table.concat(table.map(remove_suits, function(suit)
              return Fk:translate(Card.getSuitString({ suit = suit }, true))
            end), "/"), #remove_suits, data.from:__touistring())
      end
    end
    if #cards > 0 then
      local ids = room:askToChooseCardsAndChoice(player, {
        cards = cards,
        skill_name = skel.name,
        prompt = prompt,
        min_num = 1,
        max_num = 1,
      })
      if #ids > 0 then
        room:sendLog {
          type = "#lb__qinwei_log",
          from = player.id,
          to = { to.id },
          arg = #cards,
          arg2 = Fk:getCardById(ids[1]).trueName,
          toast = true,
        }
      end
    end
    if player:hasSkill("lb__yunhui", true) then
      if #insert_suits > 0 then
        table.insertTable(suits, insert_suits)
        for _, suit in ipairs(insert_suits) do
          room:addTableMarkIfNeed(player, "@[suits]lb__qinwei-turn", suit)
        end
      end
      room:setPlayerMark(player, "@[suits]lb__yunhui", #suits == 0 and 0 or suits)  -- 放上来安全点
      if #remove_suits > 0 then
        room:addTableMarkIfNeed(player, "@[player]lb__qinwei-turn", data.from.id)
        player:drawCards(#remove_suits, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  is_delay_effect = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and table.contains(player:getTableMark("@[player]lb__qinwei-turn"), target.id)
        and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
  end,
  on_use = function(self, event, target, player, data)
    data.additionalDamage = (data.additionalDamage or 0) + 1
    player.room:removeTableMark(player, "@[player]lb__qinwei-turn", target.id)
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function(self, player, skill, card, to)
    for _, owner in ipairs(Fk:currentRoom().alive_players) do
      if card and owner:hasSkill(skel.name) and table.contains(owner:getTableMark("@[player]lb__qinwei-turn"), player.id) then
        return card.type == Card.TypeBasic or card:isCommonTrick()
      end
    end
  end,
})

skel:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
      return #e.data.tos > 0
    end, 0)[1]
    if lastUseEvent == nil then return false end
    local last_use = lastUseEvent.data
    player.room:setPlayerMark(player, "@lb__qinwei", last_use.tos[1] or 0)
  end,
})

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb__qinwei", 0)
  player.room:setPlayerMark(player, "@[player]lb__qinwei-turn", 0)
  player.room:setPlayerMark(player, "@[suits]lb__qinwei-turn", 0)
end)

return skel